#!/usr/bin/env python
# coding=utf-8
from flask import Blueprint

from plant import logger
from plant.base.beanret import BeanRet
from plant.base.tools.config import Config
from plant.service.moisturesv import GroveMoistureSensor

pitop = Blueprint('moisture', __name__)


@pitop.route('/moisture/read')
def read():
    '''
    read tem and humi
    :return:
    '''
    pin = int(Config().read("moisture", "pin"))
    sensor = GroveMoistureSensor(int(pin))
    value = sensor.moisture
    if 0 <= value and value < 300:
        result = 'Dry'
    elif 300 <= value and value < 600:
        result = 'Moist'
    else:
        result = 'Wet'
    logger.info("moisture: " + str(value) + " " + result)
    return BeanRet(success=True, data={'value': value, 'result': result}).toJson()
